package com.chinasoft.goodsManager.dao.impl;

import com.chinasoft.goodsManager.dao.LoginDao;
import com.chinasoft.goodsManager.domain.Goods;
import com.chinasoft.goodsManager.domain.Manager;
import com.chinasoft.goodsManager.util.JDBCUtils;
import com.chinasoft.goodsManager.util.JedisUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class LoginDaoImpl implements LoginDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    private Jedis jedis = JedisUtil.getJedis();
    //初始化
    @Override
    public List<Map<String, Object>> findMUserName() {
        String sql = "select musername from g_manager";
        System.out.println(sql);
        return template.queryForList(sql);
    }
    @Override
    public List<Map<String, Object>> findMEmail() {
        String sql = "select memail from g_manager";
        return template.queryForList(sql);
    }
    @Override
    public void setRedisName(String musername) {
        jedis.sadd("musername",musername);
    }
    @Override
    public void setRedisEmail(String memail) {
        jedis.sadd("memail",memail);
    }

    //redis查询用户名
    @Override
    public Set<String> findMUserNameByName() {
        Set<String> musernames = jedis.smembers("musername");
        return musernames;
    }

    //redis查询邮箱
    @Override
    public Set<String> findMEmailByEmail() {
        Set<String> memails = jedis.smembers("memail");
        return memails;
    }

    //注册添加
    @Override
    public void addNewManager(String musername, String memail, String mpassword) {
        jedis.sadd("musername",musername);
        jedis.sadd("memail",memail);
        String sql = "insert into g_manager values (?,?,?,?,?,?,?,?,?,?)";
        template.update(sql,null,musername,mpassword,".\\login\\images\\4.jpg","未填写","未填写","未填写","未填写","未填写",memail);
    }

    @Override
    public Manager findMUserNameAndPassword(String musername, String mpassword) {
        try {
            String sql = "select * from g_manager where musername = ? and mpassword = ?";
            List<Manager> query = template.query(sql, new BeanPropertyRowMapper<>(Manager.class),musername,mpassword);
            if (query.size()>1){
                return null;
            }else {
                return query.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Manager findEmailAndPassword(String memail, String mpassword) {
        try {
            String sql = "select * from g_manager where memail = ? and mpassword = ?";
            List<Manager> query = template.query(sql, new BeanPropertyRowMapper<>(Manager.class),memail,mpassword);
            if (query.size()>1){
                return null;
            }else {
                return query.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Manager updateManagerInformation(Manager manager) {
        try {
            String sql1 = "update g_manager set mname = ?,mage = ?,mcity = ?,maddress = ?,mphone = ? where id = ?";
            int i = template.update(sql1,manager.getMname(),manager.getMage(),manager.getMcity(),manager.getMaddress(),manager.getMphone(),manager.getId());
            String sql2 = "select * from g_manager where id = ?";
            List<Manager> query = template.query(sql2, new BeanPropertyRowMapper<>(Manager.class),manager.getId());
            if (query.size()>1){
                return null;
            }else {
                return query.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Manager updateManagerImg(String filename, Integer id) {
        try {
            String sql1 = "update g_manager set mimg = ? where id = ?";
            int i = template.update(sql1,filename,id);
            String sql2 = "select * from g_manager where id = ?";
            List<Manager> query = template.query(sql2, new BeanPropertyRowMapper<>(Manager.class),id);
            if (query.size()>1){
                return null;
            }else {
                return query.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Goods findGoodsById(Integer id) {
        try {
            String sql = "select * from g_goods where id = ?";
            List<Goods> query = template.query(sql, new BeanPropertyRowMapper<>(Goods.class),id);
            if (query.size()>1){
                return null;
            }else {
                return query.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void addGoods(Goods goods) {
        String sql = "insert into g_goods values (?,?,?,?,?,?,?)";
        template.update(sql,null,goods.getGimg(),goods.getGname(),goods.getGnum(),goods.getGpprice(),goods.getGprice(),goods.getGdescrike());
    }

    @Override
    public void updateGoods(Goods goods) {
        String sql = "update g_goods set gimg = ?,gname = ?,gnum = ?,gpprice = ?,gprice = ?,gdescrike = ? where id = ?";
        template.update(sql,goods.getGimg(),goods.getGname(),goods.getGnum(),goods.getGpprice(),goods.getGprice(),goods.getGdescrike(),goods.getId());
    }
}
